package com.tulele.vtool.handler;

import com.google.common.collect.Maps;
import com.tulele.vtool.model.ArticleInfo;
import com.tulele.vtool.model.BArticleIllustrate;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.BArticleIllustrateService;
import com.tulele.vtool.service.IArticleInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 文章插入图片表(BArticleIllustrate)表控制层
 *
 * @author makejava
 * @since 2020-09-29 09:22:06
 */
@Slf4j
@RestController
public class BArticleIllustrateHandler {
    /**
     * 服务对象
     */
    @Autowired
    private BArticleIllustrateService bArticleIllustrateService;

    @Autowired
    private IArticleInfoService articleInfoService;

    /**
     * 通过实体插入单行数据
     *
     * @param bArticleIllustrate
     * @return 单条数据
     */
    @PostMapping(value = "/insertOneByIllustrate", produces = "application/json;charset=UTF-8")
    public RespData insertOneByIllustrate(@RequestHeader("userId") String userId, @RequestBody BArticleIllustrate bArticleIllustrate) {
        RespData respData = new RespData();

        Integer articleId = bArticleIllustrate.getArticleId();

        //查询文章ID信息，判断是不是用户自己的文章，并且是模板应用于本篇文章，则将文章添加为自己的文章
        ArticleInfo articleInfoInDB = articleInfoService.getArticleDetailInfo(articleId);

        if (articleInfoInDB.getUserId() == null || articleInfoInDB.getUserId().intValue() != Integer.parseInt(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        if (bArticleIllustrate == null || bArticleIllustrate.getArticleId() == null || bArticleIllustrate.getIllustrateUrl() == null || bArticleIllustrate.getIllustrateType() == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        if (!(bArticleIllustrate.getIllustrateType() == 1 || bArticleIllustrate.getIllustrateType() == 2)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        BArticleIllustrate bArticleIllustrateVo = new BArticleIllustrate();
        bArticleIllustrateVo.setArticleId(bArticleIllustrate.getArticleId());
        bArticleIllustrateVo.setIllustrateType(bArticleIllustrate.getIllustrateType());

        List<BArticleIllustrate> bArticleIllustrateList = this.bArticleIllustrateService.queryAll(bArticleIllustrateVo);

        //先删除
//        if (!CollectionUtils.isEmpty(bArticleIllustrateList)){
        if (CollectionUtils.isNotEmpty(bArticleIllustrateList)) {
            bArticleIllustrateList.forEach(info -> {
                this.bArticleIllustrateService.deleteById(info.getId());
            });
        }

        bArticleIllustrate.setCreateTime(new Date());//创建时间
        bArticleIllustrate.setUpdateTime(new Date());//更新时间

        if("".equals(bArticleIllustrate.getIllustrateUrl())){
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("操作成功");
            return respData;
        }

        //再添加
        BArticleIllustrate bArticleIllustrateService = this.bArticleIllustrateService.insert(bArticleIllustrate);

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        if (bArticleIllustrateService != null) {
            resultMap.put("bArticleIllustrateService", bArticleIllustrateService);
            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("插入成功");
            return respData;
        }

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("插入失败");
        return respData;
    }

    /**
     * 通过主键删除数据
     *
     * @param bArticleIllustrate 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/deleteById", produces = "application/json;charset=UTF-8")
    public RespData deleteById(@RequestHeader("userId") String userId, @RequestBody BArticleIllustrate bArticleIllustrate) {
        RespData respData = new RespData();

        if (bArticleIllustrate == null || bArticleIllustrate.getId() == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Integer id = bArticleIllustrate.getId();
        boolean isDelete = this.bArticleIllustrateService.deleteById(id);

        if (isDelete) {
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("删除成功");
            return respData;
        }

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("删除失败");
        return respData;
    }

    /**
     * 通过实例对象查询单条数据
     *
     * @param bArticleIllustrate 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/queryByIllustrate", produces = "application/json;charset=UTF-8")
    public RespData queryByIllustrate(@RequestHeader("userId") String userId, @RequestBody BArticleIllustrate bArticleIllustrate) {
        RespData respData = new RespData();

        if (bArticleIllustrate == null || bArticleIllustrate.getArticleId() == null || bArticleIllustrate.getIllustrateType() == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        if (!(bArticleIllustrate.getIllustrateType() == 1 || bArticleIllustrate.getIllustrateType() == 2)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        BArticleIllustrate bArticleIllustrateService = this.bArticleIllustrateService.queryByEntity(bArticleIllustrate);

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(2);
        if (bArticleIllustrateService != null) {
            resultMap.put("illustrateId", bArticleIllustrateService.getId());
            resultMap.put("illustrateUrl", bArticleIllustrateService.getIllustrateUrl());
            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("查询成功");
            return respData;
        }

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("暂无更多");
        return respData;
    }
}
